<?php
    $url="localhost:3306";
    $user="root";
    $password="";
    $database="php";
    $conn=mysqli_connect($url,$user,$password,$database);
    // var_dump($conn);
    if(!$conn){
        echo "error-msg:" . mysqli_connect_error();
        echo '<br>';
        echo "error-code:" . mysqli_connect_errno();
        echo '<br>';
        return;
    }
    echo "连接成功";
    echo '<br>';
    mysqli_query($conn,"set names utf8");

    $name = "考拉2";
    $price = 35;
    $unit = "斤";
    $imageUrl = "/day03/images/Koala.jpg";
    // 2.执行sql语句(增删改)
    $sql = "insert into product " 
            ." (p_name,p_price,p_unit,p_image_url) "
            ." values " 
            ." ('{$name}', {$price}, '{$unit}', '{$imageUrl}')";
    // echo "<div>{$sql}</div>";

    // 执行增删改, 成功返回true, 失败返回false。
    $result = mysqli_query($conn, $sql);
    if($result){
        echo "<div>数据添加成功</div>";
        $id=mysqli_insert_id($conn);
        echo "<div>新添数据主键为：{$id}</div>";
    }else{
        echo "error-msg:" . mysqli_error($conn);
    echo "<br>";
    echo "error-code:" . mysqli_errno($conn);
    echo "<br>";
    }
    mysqli_close($conn);
?>